﻿using System;
using System.Xml;

namespace Ro.AssistFunction.BackNodeInfo
{
    /// <summary>
    /// 提取需要的信息
    /// </summary>
    public class ExtractNeedInfo
    {
        /// <summary>
        /// 返回超时
        /// </summary>
        /// <param name="sigNode">含有超时的节点</param>
        /// <returns>返回TimeSpan</returns>
        public TimeSpan ExtractTimeout(XmlNode sigNode)
        {
            TimeSpan timeoutSpan = TimeSpan.FromSeconds(15);
            //本身没有attribute类型
            if (sigNode.Attributes != null)
                foreach (XmlAttribute sigatt in sigNode.Attributes)
                {
                    if (sigatt.Name == "web:Timeout")
                    {
                        timeoutSpan = TimeSpan.FromSeconds(Convert.ToInt32(sigatt.Value));
                        break;
                    }
                    timeoutSpan = TimeSpan.FromSeconds(15);
                }
            else
                timeoutSpan = TimeSpan.FromSeconds(15);
            return timeoutSpan;
        }

        /// <summary>
        /// 返回是否修改为警告
        /// </summary>
        /// <param name="sigNode">含有警告的节点</param>
        /// <returns>返回Boolean值</returns>
        public bool ExtractTreatBool(XmlNode sigNode)
        {
            bool treatBool = false;
            //本身没有attribute类型
            if (sigNode.Attributes != null)
            {
                //如果有attribute节点,需要判断是否存在对应的attname节点
                foreach (XmlAttribute sigatt in sigNode.Attributes)
                {
                    if (sigatt.Name == "tcs:TreatErrorsAsWarnings")
                    {
                        treatBool = Convert.ToBoolean(sigatt.Value);
                        break;
                    }
                }
            }
            return treatBool;
        }

        /// <summary>
        /// 返回AttributeValue值
        /// </summary>
        /// <param name="sigNode">需要提取信息的节点</param>
        /// <param name="attname">attribute名称</param>
        public string ExtractAttributeValue(XmlNode sigNode, string attname)
        {
            string sendValue = String.Empty;
            //本身没有attribute类型
            if (sigNode.Attributes == null)
            {
                sendValue = String.Empty;
            }

            else
            {
                //如果有attribute节点,需要判断是否存在对应的attname节点
                foreach (XmlAttribute sigatt in sigNode.Attributes)
                {
                    if (sigatt.Name == attname)
                    {
                        sendValue = sigatt.Value;
                        break;
                    }
                    sendValue = String.Empty;
                }
            }
            return sendValue;
        }


        /// <summary>
        /// 返回InnerText值
        /// </summary>
        /// <param name="sigNode">总节点即可</param>
        /// <param name="attname">InnerText的节点名称</param>
        public string ExtractInnerText(XmlNode sigNode, string attname)
        {
            string sendValue = String.Empty;

            //本身没有InnerText类型
            if (string.IsNullOrEmpty(sigNode.InnerText))
            {
                sendValue = String.Empty;
            }

            else
            {
                //如果有attribute节点,需要判断是否存在对应的attname节点
                foreach (XmlNode sig in sigNode.ChildNodes)
                {
                    if (sig.Name == attname)
                    {
                        sendValue = sig.InnerText;
                        break;
                    }
                    //默认值
                    sendValue = String.Empty;
                }
            }
            return sendValue;
        }
    }
}
